// 基于vue-router 封装的路由文件 和 路由拦截
import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/index",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../view/login"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../view/register"),
    },
    {
      path: "/index",
      name: "index",
      component: () => import("../view/index"),
    },
    {
      path: "/news",
      name: "news",
      component: () => import("../view/news"),
    },
    {
      path: "/newsDetails",
      name: "newsDetails",
      component: () => import("../view/newsDetails"),
    },
    {
      path: "/goodsList",
      name: "goodsList",
      component: () => import("../view/goodsList"),
    },
    {
      path: "/goodsDetails",
      name: "goodsDetails",
      component: () => import("../view/goodsDetails"),
    },
    {
      path: "/shoppingCart",
      name: "shoppingCart",
      component: () => import("../view/shoppingCart"),
    },
    {
      path: "/paymentOrder",
      name: "paymentOrder",
      component: () => import("../view/paymentOrder"),
    },
    {
      path: "/classify",
      name: "classify",
      component: () => import("../view/classify"),
    },
    {
      path: "/vip",
      name: "vip",
      component: () => import("../view/vip"),
    },
    {
      path: "/orders",
      name: "orders",
      component: () => import("../view/orders"),
    },
    {
      path: "/myFavorite",
      name: "myFavorite",
      component: () => import("../view/myFavorite"),
    },
    {
      path: "/addressManage",
      name: "addressManage",
      component: () => import("../view/addressManage"),
    },
    {
      path: "/addAdderss",
      name: "addAdderss",
      component: () => import("../view/addAdderss"),
    },
    {
      path: "/userInfo",
      name: "userInfo",
      component: () => import("../view/userInfo"),
    },
    {
      path: "/changePwd",
      name: "changePwd",
      component: () => import("../view/changePwd"),
    },
  ],
});

// 路由跳转前匹配登录
router.beforeEach((to, from, next) => {
  if (window.localStorage.getItem("u_login") && window.localStorage.getItem("u_id")) {
    // 如果登录标志有值直接跳转
    next();
  } else {
    // interceptRouter 是拦截未登录不进入的路由
    let interceptRouter = ["shoppingCart", "vip"];
    if (interceptRouter.find(val=>val === to.name)) {
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
